package FileEditor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class FileEditor extends JFrame {
    private JTextField filePathField;
    private JTextArea contentArea;
    private JScrollPane scrollPane;
    private JButton browseButton, clearButton, readButton, saveButton, exitButton;

    public FileEditor() {
        initializeComponents();
        setLayout(new BorderLayout());

        // 添加文件路径字段到北边
        JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        northPanel.add(filePathField);
        northPanel.add(browseButton);
        add(northPanel, BorderLayout.NORTH);

        // 添加内容区域到中心
        add(scrollPane, BorderLayout.CENTER);

        // 添加按钮面板到南边
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(clearButton);
        buttonPanel.add(readButton);
        buttonPanel.add(saveButton);
        buttonPanel.add(exitButton);
        add(buttonPanel, BorderLayout.SOUTH);

        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void initializeComponents() {
        filePathField = new JTextField("File:", 30); // 增加宽度
        filePathField.setForeground(Color.GRAY); // 设置初始文本颜色为灰色
        filePathField.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                if (filePathField.getText().equals("File:")) {
                    filePathField.setText("");
                    filePathField.setForeground(Color.BLACK); // 获得焦点时清除占位符文本
                }
            }

            @Override
            public void focusLost(java.awt.event.FocusEvent evt) {
                if (filePathField.getText().isEmpty()) {
                    filePathField.setText("File:");
                    filePathField.setForeground(Color.GRAY); // 失去焦点时恢复占位符文本
                }
            }
        });

        contentArea = new JTextArea(10, 30);
        scrollPane = new JScrollPane(contentArea);
        browseButton = new JButton("Browse");
        clearButton = new JButton("Clear");
        readButton = new JButton("Read");
        saveButton = new JButton("Save");
        exitButton = new JButton("Exit");

        // 设置动作监听器
        browseButton.addActionListener(e -> browseFile());
        clearButton.addActionListener(e -> contentArea.setText(""));
        readButton.addActionListener(e -> readFile());
        saveButton.addActionListener(e -> saveFile());
        exitButton.addActionListener(e -> System.exit(0));
    }

    private void browseFile() {
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            filePathField.setText(selectedFile.getAbsolutePath());
        }
    }

    private void readFile() {
        String filePath = filePathField.getText();
        File file = new File(filePath);
        if (file.exists()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                StringBuilder content = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    content.append(line).append("\n");
                }
                contentArea.setText(content.toString());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Error reading file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "File does not exist.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void saveFile() {
        String filePath = filePathField.getText();
        File file = new File(filePath);
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(contentArea.getText());
            JOptionPane.showMessageDialog(this, "File saved successfully.", "Success", JOptionPane.INFORMATION_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Error saving file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new FileEditor());
    }
}
